package com.example.jbmsuppliers.sinopec.doorjurisd;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.ICertAddress;
import com.jbm.jbmsupplier.util.IEGuardKey;
import com.jbm.jbmsupplier.util.IGeoinfo;
import com.jbm.jbmsupplier.util.IGeoinfoCtrl;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class IdentificationStepTwoActivity extends ActivityUse {

    String mCardNo;
    Spinner mSpinnerQu, mSpinnerLi, mSpinnerLou, mSpinnerMen;
    LinearLayout mLayoutLi, mLayoutLou, mLayoutMen;
    ArrayList<IGeoinfo> mQuArray = new ArrayList<IGeoinfo>(),
            mLiArray = new ArrayList<IGeoinfo>(),
            mLouArray = new ArrayList<IGeoinfo>(),
            mMenArray = new ArrayList<IGeoinfo>();
    int level = 0;
    int mId = 0;
    String mQu = null, mLi = null, mLou = null, mMen = null;
    EditText mEditHouseNo = null;
    @InjectView(R.id.identification_edit_name)
    EditText identificationEditName;
    @InjectView(R.id.identification_edit_cade)
    EditText identificationEditCade;
    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    private DialogLoading loading, loadings;
    public final static int Cmd_GetGeoInfos = 0001;
    public final static int Cmd_Add_AddressforKeys = 0002;
    public final static int Cmd_RegistKeyWithIDCards = 0003;
    public final static int EXETS = 0004;

    @Override
    protected int setContentView1() {
        return R.layout.activity_identification_step_two;
    }

    @Override
    protected void initView() {
        ExitAppUtlis.getInstance().addActivity(this);
        ButterKnife.inject(this);
        headerTitle.setText("开门认证");
        headerRight.setText("认证");
        Intent intent = getIntent();
        this.mCardNo = intent.getStringExtra("cardno");
        NetWork netWork = new NetWork(mHandler);
        loading = new DialogLoading(IdentificationStepTwoActivity.this, "验证中...", true);
        loading.show();
        netWork.GetGeoinfoList(null, "");
        this.mSpinnerQu = (Spinner) this.findViewById(R.id.identification_spinner_qu);
        this.mSpinnerLi = (Spinner) this.findViewById(R.id.identification_spinner_li);
        this.mSpinnerLou = (Spinner) this.findViewById(R.id.identification_spinner_lou);
        this.mSpinnerMen = (Spinner) this.findViewById(R.id.identification_spinner_men);
        this.mLayoutLi = (LinearLayout) this.findViewById(R.id.identification_llayout_li);
        this.mLayoutLou = (LinearLayout) this.findViewById(R.id.identification_llayout_lou);
        this.mLayoutMen = (LinearLayout) this.findViewById(R.id.identification_llayout_men);
        this.mEditHouseNo = (EditText) this.findViewById(R.id.identification_edit_houseno);
    }


    private ArrayAdapter<String> adapter;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message paramMessage) {
            try {
                switch (paramMessage.what) {
                    case EXETS: {
                        setResult(RESULT_OK);
                        finish();
                        break;
                    }
                    case Cmd_RegistKeyWithIDCards: {
                        IEGuardKey key = (IEGuardKey) paramMessage.obj;
                        if (key == null) {
                            if (loadings != null) {
                                loadings.dismiss();
                            }
                            return;
                        }
                        if (!key.back) {

                            if (loadings != null) {
                                loadings.dismiss();
                            }
                            hint(key.errorinfo, mHandler, EXETS);
                            return;
                        }
                        if (loadings != null) {
                            loadings.dismiss();
                        }
                        hint(key.errorinfo, mHandler, EXETS);
                        break;
                    }
                    case Cmd_Add_AddressforKeys: {
                        ICertAddress certAddress = (ICertAddress) paramMessage.obj;
                        if (certAddress == null)
                            return;
                        if (!certAddress.back)
                            break;
                        NetWork netWork = new NetWork(mHandler);
                        netWork.RegistKeyWithIDCard(certAddress.caddr_id, mCardNo);
                    }
                    break;
                    case Cmd_GetGeoInfos: {
                        IGeoinfoCtrl ctrl = (IGeoinfoCtrl) paramMessage.obj;
                        if (ctrl == null) {
                            if (loading != null) {
                                loading.dismiss();
                            }
                            hint(ctrl.errorinfo, mHandler, EXETS);
                            return;
                        }
                        if (!ctrl.back) {

                            if (loading != null) {
                                loading.dismiss();
                            }
                            hint(ctrl.errorinfo, mHandler, EXETS);
                            return;
                        }
                        if (loading != null) {
                            loading.dismiss();
                        }
                        final ArrayList<String> names = new ArrayList<String>();
                        Spinner useSpinner = null;
                        for (int i = 0; i < ctrl.count; i++) {
                            IGeoinfo geoinfo = ctrl.IndexofList(i);

                            names.add(geoinfo.gi_name);
                            switch (level) {
                                case 0:
                                    mQuArray.add(geoinfo);
                                    useSpinner = mSpinnerQu;
                                    break;
                                case 1:

                                    mLiArray.add(geoinfo);
                                    useSpinner = mSpinnerLi;
//                                    mLayoutLi.setVisibility(View.VISIBLE);
                                    break;
                                case 2:

                                    mLouArray.add(geoinfo);
                                    useSpinner = mSpinnerLou;
//                                    mLayoutLou.setVisibility(View.VISIBLE);
                                    break;
                                case 3:
                                    mMenArray.clear();
                                    mMenArray.add(geoinfo);
                                    useSpinner = mSpinnerMen;
//                                    mLayoutMen.setVisibility(View.VISIBLE);
                                    break;
                            }
                        }
                        String[] mItems = (String[]) names.toArray(new String[names.size()]);
                        adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, mItems);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        if (useSpinner == null) {
                            //ShowError("内部错误，无法显示信息！");
                            return;
                        }
                        useSpinner.setAdapter(adapter);
                        useSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                try {
                                    if (position < 0)
                                        return;
                                    if (mSpinnerQu == parent)
                                        level = 1;
                                    if (mSpinnerLi == parent)
                                        level = 2;
                                    if (mSpinnerLou == parent)
                                        level = 3;
                                    if (mSpinnerMen == parent)
                                        level = 4;
                                    String name = names.get(position);
                                    ArrayList<IGeoinfo> clickArray = null;
                                    int checkInfo = -1;
                                    switch (level) {
                                        case 1:
                                            clickArray = mQuArray;
                                            checkInfo = 1;
                                            mQu = name;
                                            break;
                                        case 2:
                                            clickArray = mLiArray;
                                            checkInfo = 2;
                                            mLi = name;
                                            break;
                                        case 3:
                                            clickArray = mLouArray;
                                            checkInfo = 3;
                                            mLou = name;
                                            break;
                                        case 4:
                                            clickArray = mMenArray;
                                            checkInfo = 0;
                                            mMen = name;
                                            break;

                                    }
                                    if (clickArray == null) {

//                                    setAlertDialogqd("内部错误，不能响应点击事件！");
                                        return;
                                    }
                                    IGeoinfo geoinfo = clickArray.get(position);

                                    if (geoinfo == null)
                                        return;
                                    if (checkInfo <= 0)
                                        return;
                                    mId = geoinfo.gi_id;
                                    String type = "";
                                    switch (checkInfo) {
                                        case 1:
                                            mLiArray.clear();
                                            type = "village";
                                            break;
                                        case 2:
                                            mLouArray.clear();
                                            break;
                                        case 3:
                                            mMenArray.clear();
                                            break;
                                    }
                                    NetWork netWork = new NetWork(mHandler);
                                    netWork.GetGeoinfoList(geoinfo, type);
                                } catch (Exception e) {
                                    return;
                                }
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                        break;
                    }
                }
            } catch (Exception e) {
                String msg = e.getMessage();
            }
        }

    };

    @OnClick({R.id.header_right, R.id.header_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_right:
                if (mQu == null || mLi == null || mLou == null) {
                    hint("请选择对应实际使用地址！");
                    return;
                }
                if (level != 3) {
                    hint("请选择至楼的信息");
                    return;
                }
                if (mEditHouseNo.getText().toString().equals("")) {
                    hint("请输入房间号码！");
                    return;
                }
                String address = mQu + mLi + mLou + mEditHouseNo.getText().toString();
                NetWork netWork = new NetWork(mHandler);
                netWork.AddAddress(address, mId);
                loadings = new DialogLoading(IdentificationStepTwoActivity.this, "验证中...", true);
                loadings.show();
//                if (identificationEditName.getText().toString().equals("")) {
//                    hint("请输入个人信息！");
//
//                }
//                if (identificationEditCade.getText().toString().equals("")) {
//                    hint("请输入个人信息！");
//
//                } else if (identificationEditCade.getText().toString().length() != 18) {
//                    hint("请核实你的身份证！");
//
//
//                } else
//                    try {
//                    if (!IdCardDetection.IDCardValidate(identificationEditCade.getText().toString()).equals("")) {
//                        hint("请核实你的身份证！");
//                    } else {



//                    }
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }
                break;
            case R.id.header_back:
                finish();
                break;
        }
    }
}
